package com.lenovo.smarttraffic.ui.activity.stage3;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.WeatherBean;
import com.lenovo.smarttraffic.database.SensorDataDB;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.util.HttpUtils;

import org.litepal.LitePal;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class WeatherActivity extends BaseActivity {

    private TextView date_tv, temp_tv;

    private RecyclerView forecast_rv, life_rv;

    private Gson gson = new Gson();

    private Handler handler = new Handler();

    @Override
    protected int getLayout() {
        return R.layout.activity_weather_info;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initToolBar(this, "天气信息");

        date_tv = findViewById(R.id.date_tv);
        temp_tv = findViewById(R.id.temp_tv);

        forecast_rv = findViewById(R.id.forecast_rv);
        life_rv = findViewById(R.id.life_rv);

        HttpUtils.post("get_weather", new HttpUtils().setParam("UserName", MainActivity.userName), response -> {
            WeatherBean weatherBean = gson.fromJson(response.toString(), WeatherBean.class);
            if (weatherBean.getROWS_DETAIL() != null) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 EEEE", Locale.CHINA);
                date_tv.setText(simpleDateFormat.format(new Date()));
                temp_tv.setText("九江" + weatherBean.getWCurrent() + "℃");
                forecast_rv.setAdapter(new ForecastRvAdapter(weatherBean));

                runnable.run();
                refresh();
            }
        });

    }

    public void refresh() {
        handler.postDelayed(() -> {
            runnable.run();
            refresh();
        }, 3000);
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            life_rv.setAdapter(new LifeRvAdapter(LitePal.order("date desc").find(SensorDataDB.class)));
        }
    };

}

class ForecastRvAdapter extends RecyclerView.Adapter<ForecastRvAdapter.ViewHolder> {
    private WeatherBean weatherBean;
    private View view;

    public ForecastRvAdapter(WeatherBean weatherBean) {
        this.weatherBean = weatherBean;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item_env_forecast, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        WeatherBean.ROWSDETAILBean rowsdetailBean = weatherBean.getROWS_DETAIL().get(position);
        String day = rowsdetailBean.getWData().split("-")[2] + "日";
        switch (position) {
            case 0:
                holder.date.setText(day + ("(昨天)"));
                break;
            case 1:
                holder.date.setText(day + ("(今天)"));
                break;
            case 2:
                holder.date.setText(day + ("(明天)"));
                break;
            case 3:
                holder.date.setText(day + ("(后天)"));
                break;
            case 4:
                holder.date.setText(day + "(周四)");
                break;
            case 5:
                holder.date.setText(day + "(周五)");
                break;
            case 6:
                holder.date.setText(day + "(周六)");
                break;
        }
        holder.temp.setText(rowsdetailBean.getTemperature().replace("~", "/") + "℃");
        holder.img.setImageResource(getImg(position));
        holder.type.setText(getType(position));
    }

    private int getImg(int pos) {
        switch (pos % 2) {
            case 0:
                return R.mipmap.w001;
            case 1:
                return R.mipmap.w002;
            default:
                return R.mipmap.w003;
        }
    }

    private String getType(int pos) {
        switch (pos % 2) {
            case 0:
                return "晴";
            case 1:
                return "多云";
            default:
                return "小雨";
        }
    }

    @Override
    public int getItemCount() {
        return weatherBean.getROWS_DETAIL().size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView img;
        private TextView date, type, temp;

        public ViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.image);
            date = itemView.findViewById(R.id.item_date);
            type = itemView.findViewById(R.id.item_type);
            temp = itemView.findViewById(R.id.item_temp);
        }
    }
}

class LifeRvAdapter extends RecyclerView.Adapter<LifeRvAdapter.ViewHolder> {
    private List<SensorDataDB> list;
    private View view;

    public LifeRvAdapter(List<SensorDataDB> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item_env_life, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.img.setImageResource(getImg(position));
        holder.title.setText(getTitle(position));

        int value = 0;
        switch (position) {
            case 0:
                value = Double.valueOf(list.get(0).getLightIntensity()).intValue();
                break;
            case 1:
            case 2:
                value = Double.valueOf(list.get(0).getTemperature()).intValue();
                break;
            case 3:
                value = Double.valueOf(list.get(0).getCo2()).intValue();
                break;
            case 4:
                value = Double.valueOf(list.get(0).getPm25()).intValue();
                break;
        }
        setText(holder, position, value);
    }

    private void setText(ViewHolder holder, int pos, int value) {
        String level = "", desc = "";
        switch (pos) {
            case 0:
                if (value >= 0 && value <= 1000) {
                    level = "弱";
                    desc = "辐射较弱，涂擦SPF12~15、PA+护肤品";
                } else if (value > 1000 && value < 3000) {
                    level = "中等";
                    desc = "涂擦SPF大于15、PA+防晒护肤品";
                } else {
                    level = "强";
                    desc = "尽量减少外出，需要涂抹高倍数防晒霜";
                }
                break;
            case 1:
                if (value < 8) {
                    level = "较易发";
                    desc = "温度低，风较大，较易发生感冒，注意防护";
                } else {
                    level = "少发";
                    desc = "无明显降温，感冒机率较低";
                }
                break;
            case 2:
                if (value < 12) {
                    level = "冷";
                    desc = "建议穿长袖衬衫、单裤等服装";
                } else if (value > 12 && value < 21) {
                    level = "舒适";
                    desc = "建议穿短袖衬衫、单裤等服装";
                } else {
                    level = "热";
                    desc = "适合穿T恤、短薄外套等夏季服装";
                }
                break;
            case 3:
                if (value >= 0 && value <= 3000) {
                    level = "适宜";
                    desc = "气候适宜，推荐您进行户外运动";
                } else if (value > 3000 && value < 6000) {
                    level = "中";
                    desc = "易感人群应适当减少室外活动";
                } else {
                    level = "较不宜";
                    desc = "空气氧气含量低，请在室内进行休闲运动";
                }
                break;
            case 4:
                if (value >= 0 && value <= 30) {
                    level = "优";
                    desc = "空气质量非常好，非常适合户外活动，趁机出去多呼吸新鲜空气";
                } else if (value > 30 && value < 100) {
                    level = "良";
                    desc = "易感人群应适当减少室外活动";
                } else {
                    level = "污染";
                    desc = "空气质量差，不适合户外活动";
                }
                break;
        }

        holder.level.setText(level + "(" + value + ")");
        holder.desc.setText(desc);
    }

    private int getImg(int pos) {
        switch (pos) {
            case 0:
                return R.mipmap.icon101;
            case 1:
                return R.mipmap.icon102;
            case 2:
                return R.mipmap.icon103;
            case 3:
                return R.mipmap.icon104;
            case 4:
                return R.mipmap.icon105;
            default:
                return R.mipmap.w001;
        }
    }

    private String getTitle(int pos) {
        switch (pos) {
            case 0:
                return "紫外线指数";
            case 1:
                return "感冒指数";
            case 2:
                return "穿衣指数";
            case 3:
                return "运动指数";
            case 4:
                return "空气污染扩散\n指数";
            default:
                return "";
        }
    }

    @Override
    public int getItemCount() {
        return 5;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView img;
        private TextView title, level, desc;

        public ViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.image);
            title = itemView.findViewById(R.id.item_title);
            level = itemView.findViewById(R.id.item_level);
            desc = itemView.findViewById(R.id.item_desc);
        }
    }
}
